package baoxin.com.robotcoach;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.ui.RecognizerDialogListener;

import org.json.JSONObject;

import baoxin.com.robotcoach.utils.NetUtil;
import baoxin.com.robotcoach.utils.TtsListener;

public class LoginActivity extends BottomActivity {


    /**
     * 听写UI监听器
     */
    private RecognizerDialogListener mRecognizerDialogListener;
    private EditText phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        super.onCreate(savedInstanceState);
        phone = findViewById(R.id.et_phone);
        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //     TODO if (s.toString().length()==11){执行登录操作}
            }
        });

        mRecognizerDialogListener = new RecognizerDialogListener() {
            public void onResult(RecognizerResult results, boolean isLast) {

                final String phoneNum = TtsListener.printResult(results);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        phone.setText(phoneNum);
                        phone.setSelection(phoneNum.length());
                    }
                });

            }

            /**
             * 识别回调错误.
             */
            public void onError(SpeechError error) {

            }

        };

    }

    @Override
    protected void onSpeak() {
        checkAudioRecordPermission();

    }

    private void checkAudioRecordPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            //校验是否已具有模糊定位权限
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.RECORD_AUDIO)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.RECORD_AUDIO}, 1);
            } else {
                //具有权限
                TtsListener.listener(this, mRecognizerDialogListener);

            }
        } else {
            //系统不高于6.0直接执行
            TtsListener.listener(this, mRecognizerDialogListener);

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    TtsListener.listener(this, mRecognizerDialogListener);
                } else {
                    Toast.makeText(this, "程序正常运行需要麦克风权限", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    String url = "http://192.168.31.63:9999/zhjp/l/register/register.shtml";//注册
    String url2 = "http://192.168.31.63:9999/zhjp/l/login/login.shtml";//登陆
  //  String url2 = "http://192.168.31.63:9999/zhjp/cxxc/group/overall/list.shtml";
    String url3 = "http://192.168.31.63:9999/zhjp/cxxc/school/detailed/list.shtml";
    String url4 = "http://192.168.31.63:9999/zhjp/cxxc/school/add/reserved.shtml";
    String url5 = "http://192.168.31.63:9999/zhjp/cxxc/group/tuxedo.shtml";
    String url6 = "http://192.168.31.63:9999/zhjp/cxxc/school/add/reserved.shtml";



    String andy = "http://192.168.31.153:9191/zhjp/coach/order/coach/schedule.shtml";
    String json,json2,json3,json4,json5;

    public void onLogin(View view) {
        try {
            JSONObject user = new JSONObject();
            user.put("pswd", "123456");
            user.put("phone","17333110904");
            json = user.toString();

            JSONObject local2 = new JSONObject();
            local2.put("lat",31.864942016);
            local2.put("lon",117.2882028929);
            json2 = local2.toString();

            JSONObject local3 = new JSONObject();
            local3.put("code","xa0001");
            local3.put("lat",31.864942016);
            local3.put("lon",117.2882028929);
            json3 = local3.toString();


            JSONObject local4 = new JSONObject();
            local4.put("phone","15156091154");
            local4.put("code","xa0001");
            local4.put("ocgReNumber","4");
            local4.put("ocgId","8");
            local4.put("ocId","10");
            local4.put("ouId","4");
            json4 = local4.toString();

            JSONObject local5 = new JSONObject();
            local5.put("phone","15156091154");
            local5.put("code","xa0001");
            json5 = local5.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        String name = phone.getText().toString();
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                 //   NetUtil.post(url2, json);
                    NetUtil.get(andy);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i("junjie",e.getMessage());
                }
            }
        };
        thread.start();
        Intent it = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(it);
        finish();
    }
}
